package com.kproduce.roundcorners;

import com.kproduce.roundcorners.util.RoundHelper;
import com.kproduce.roundcorners.util.RoundMethodInterface;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

/**
 * @author kuanggang on 2019/12/10
 */
public class RoundTextView extends Text implements Component.TouchEventListener, RoundMethodInterface {

    private RoundHelper mHelper = new RoundHelper();

    public RoundTextView(Context context) {
        this(context, null);
    }

    public RoundTextView(Context context, AttrSet attrs) {
        this(context, attrs, "-1");
        invalidate();
    }

    public RoundTextView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setTouchEventListener(this::onTouchEvent);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        // 此处用onclick亦可
        if (TouchEvent.PRIMARY_POINT_DOWN == touchEvent.getAction()) {
            ShapeElement shapeElement = (ShapeElement) this.getBackgroundElement();
            shapeElement.setStroke(10, RgbColor.fromArgbInt(Color.GREEN.getValue()));
            shapeElement.setCornerRadiiArray(new float[]{35, 35, 0, 0, 0, 0, 0, 0});
            this.setBackground(shapeElement);

            ShapeElement forGround = new ShapeElement();
            forGround.setCornerRadiiArray(new float[]{30, 30, 0, 0, 0, 0, 0, 0});
            forGround.setStroke(10, RgbColor.fromArgbInt(Color.GREEN.getValue()));
            this.setForeground(forGround);
        }
        return true;
    }

    @Override
    public void setRadius(float radiusDp) {
        mHelper.setRadius(radiusDp);
    }

    @Override
    public void setRadius(float radiusTopLeftDp, float radiusTopRightDp, float radiusBottomLeftDp, float radiusBottomRightDp) {
        mHelper.setRadius(radiusTopLeftDp, radiusTopRightDp, radiusBottomLeftDp, radiusBottomRightDp);
    }

    @Override
    public void setRadiusLeft(float radiusDp) {
        mHelper.setRadiusLeft(radiusDp);
    }

    @Override
    public void setRadiusRight(float radiusDp) {
        mHelper.setRadiusRight(radiusDp);
    }

    @Override
    public void setRadiusTop(float radiusDp) {
        mHelper.setRadiusTop(radiusDp);
    }

    @Override
    public void setRadiusBottom(float radiusDp) {
        mHelper.setRadiusBottom(radiusDp);
    }

    @Override
    public void setRadiusTopLeft(float radiusDp) {
        mHelper.setRadiusTopLeft(radiusDp);
    }

    @Override
    public void setRadiusTopRight(float radiusDp) {
        mHelper.setRadiusTopRight(radiusDp);
    }

    @Override
    public void setRadiusBottomLeft(float radiusDp) {
        mHelper.setRadiusBottomLeft(radiusDp);
    }

    @Override
    public void setRadiusBottomRight(float radiusDp) {
        mHelper.setRadiusBottomRight(radiusDp);
    }

    @Override
    public void setStrokeWidth(float widthDp) {
        mHelper.setStrokeWidth(widthDp);
    }

    @Override
    public void setStrokeColor(int color) {
        mHelper.setStrokeColor(color);
    }

    @Override
    public void setStrokeWidthColor(float widthDp, int color) {
        mHelper.setStrokeWidthColor(widthDp, color);
    }


}
